<?php

namespace app\company\controller;

use app\company\validate\WarehouseValidate;
use app\service\traits\Crud;
use app\company\model\Warehouse as WarehouseModel;

class Warehouse extends Base
{
    private WarehouseModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new WarehouseModel();
        $this->validateClass = WarehouseValidate::class;
        $this->withSearch    = 'id,name';
        $this->isGs          = true;
    }

    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->goods_type  = $item->companyOrderStorages()->group("goods_rule_id")->count();//商品种类数
            $item->goods_num   = $item->companyOrderStorages()->sum("quantity");//商品数量
            $item->storage_num = $item->warehouseStorages()->count();//库位数
            $item->area_num    = $item->warehouseAreas()->count();//库区数
        });
    }

    public function detailFormat($info): mixed
    {
        $info->area_num           = $info->warehouseAreas()->count();//库区数
        $info->storage_num        = $info->warehouseStorages()->count();//库位数
        $info->storage_used_num   = $info->companyOrderStorages()->group('warehouse_storage_id')->count();//已使用库位数
        $info->storage_unused_num = $info->storage_num - $info->storage_used_num;//未使用库位数
        return $info;
    }
}